package com.spring.security.study.security_test.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登出类
 *
 * @Author: 王文龙
 * @Date: 2020/7/610:55
 * @Version: 1.0
 * @Describe: 描述:
 */
@Slf4j
@Component
public class UserLogoutSuccessHandler implements LogoutSuccessHandler {


    /**
     * 用户登出返回结果,应该让前台消除token
     *
     * @throws IOException      抛异常
     * @throws ServletException 抛异常
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("code", "200");
        resultData.put("msg", "登出成功");
        SecurityContextHolder.clearContext();
        log.info("数据:{}",resultData);
    }
}
